import { _decorator, Color, Component, Label, Node } from 'cc';
import { Condition, Tech } from '../entity/Tech';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { BuildingUpGrade } from '../village/BuildingUpGrade';
import { BManager } from '../entity/BManager';
import { Building } from '../entity/Buildings';
import { BuildingDetail } from '../village/BuildingDetail';
import { DevelopTech } from './DevelopTech';
import { UpgradeTechBuilding } from './UpgradeTechBuilding';
const { ccclass, property } = _decorator;

@ccclass('ConItem')
export class ConItem extends Component {
    @property(Label)
    conName: Label
    @property(Label)
    status: Label
    @property(Node)
    upgrade: Node

    red:Color =  new Color("#BF0E0E")
    green:Color = new Color("#57840F")

    con:Condition

    start() {
        this.upgrade.on(Node.EventType.TOUCH_END, this.onUpgrade, this)
    }

    setData(c: Condition) {
        this.con = c
        this.conName.string = `${c.name}(等级${c.lv})`

        if (c.isEnough) {
            this.status.string = '已完成'
            this.status.color = this.green
            this.upgrade.active = false
        } else {
            this.status.string = '未达成'
            this.status.color = this.red
        }
        
    }

    async onUpgrade(){
        console.log(this.con)
        if(this.con.type == 't'){
            let tech = Tech.findById(this.con.tech.id)
            if(tech){
                let ui = await UIManager.instance.openUI(PrefabEnum.UpgradeTechBuilding)
                ui.getComponent(UpgradeTechBuilding).setData(tech)
            } else {
                let ui = await UIManager.instance.openUI(PrefabEnum.DevelopTech)
                this.con.tech.level = 0
                ui.getComponent(DevelopTech).setData(this.con.tech)
            }

            return
        }




        let bm = BManager.getBManageById(this.con.building.id)
        console.log(bm)

        if(bm){
            let building = Building.createBuildingByBManager(bm)
            let ui = await UIManager.instance.openUI(PrefabEnum.BuildingUpGrade)
            ui.getComponent(BuildingUpGrade).setData(building)
        } else{
            let ui = await UIManager.instance.openUI(PrefabEnum.BuildingDetail)
            let building = Building.createByIdAndLevel(this.con.building.id, 1)
            ui.getComponent(BuildingDetail).setData(building)
        }
        
       
        
        
    }
}


